// CaptorManager.h : Defines the exported functions for the DLL application.
//

#ifndef __CAPTORMANAGER_H__
#define __CAPTORMANAGER_H__

#include "stdafx.h"
#include "ATASHD3D.h"
#include "HookD3D.h"
#include "Captor.h"
#include "DeviceHooker.h"

#include <map>
using namespace std;

struct FuncStrLt
{
	bool operator()(const char* sz1, const char* sz2) const
	{
		return strcmp(sz1, sz2) < 0;
	}
};

typedef map<const char*, CCaptor*, FuncStrLt> MAPCAPTOR;
//typedef map<const IDirect3DDevice9*, CDeviceHooker9*> MAPDEVICEHOOKER9;
typedef multimap<IUnknown*, CDeviceHookerBase*> MAPDEVICEHOOKER;

class CCriticalSection
{
	CRITICAL_SECTION m_cs;
public:
	CCriticalSection()
	{
		InitializeCriticalSection(&m_cs);
	}
	~CCriticalSection()
	{
		DeleteCriticalSection(&m_cs);
	}
	void Enter()
	{
		EnterCriticalSection(&m_cs);
	}
	void Leave()
	{
		LeaveCriticalSection(&m_cs);
	}
};

class CCaptorManager
{
private:
	static CD3dHookPresent::PROC IDirect3DSwapChain9Present;
	CD3dHookPresent m_hkrSwapChain9Present;
	IDirect3DSwapChain9* m_pSwapChain;
	MAPCAPTOR m_mapCaptors;
	MAPDEVICEHOOKER m_mapDeviceHooker;

	static CDXGIHookPresent::PROC IDXGIPresent;
	IDXGISwapChainDWM* m_pDxgiSwapChainDWM;
	CDXGIHookPresent m_hookDxgiSwapChainPresent;
public:
	CCriticalSection m_cs;

	CCaptorManager();
	~CCaptorManager();

	BOOL Create10();

	BOOL Create();
	void Destroy();
	CCaptor* CreateCaptor(LPCSTR szDeviceName);
	BOOL DestroyCaptor(CCaptor* pCaptor);
	CCaptor* CheckConnectCaptorAndDevice(IDirect3DSwapChain9* pSwapChain);
	CCaptor* CheckConnectCaptorAndDevice10(IDXGISwapChain* pSwapChain);
	void DisconnectCaptorAndDevice(CCaptor* pCaptor, IUnknown* pDevice);
	CDeviceHookerDrawPrimitive9* GetDeviceHooker9(IDirect3DDevice9* pDevice);
	CDeviceHookerBase* GetDeviceHooker10(ID3D10Device* pDevice, HOOKFUNC_DEF def);

};

void SaveTexturetoBmp(ID3D10Texture2D* pTexture, LPTSTR lpszFile);

#endif //__CAPTORMANAGER_H__
